package com.myGisChannel.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractWizardFormController;

import com.myGis.entities.Layer;
import com.myGis.entities.Project;
import com.myGis.entities.User;
import com.myGis.persistence.IPersistenceData;
import com.myGis.persistence.PersistenceData;
import com.myGis.services.IMyGisServices;
import com.myGis.services.MyGisServer;
import com.myGis.util.HibernateUtil;
import com.myGisChannel.command.CreateProjectCommand;
import com.myGisChannel.utils.AppPropertiesUtil;

@SuppressWarnings("deprecation")
public class ProjectCreateController extends AbstractWizardFormController{
	
	
	public ProjectCreateController(){
		setCommandClass(CreateProjectCommand.class);
		setCommandName("createProjectCommand");
	}

	@Override
	protected ModelAndView processFinish(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, BindException arg3)
			throws Exception {
		CreateProjectCommand cpc = (CreateProjectCommand) this.getCommand(arg0);
		System.out.println(cpc.getLonInitial());
		return null;
	}
	
	protected Map referenceData(HttpServletRequest request, Object command,	Errors errors, int page){
		System.out.println(page);
		return null;
	}
	
	protected void validatePage(Object command, Errors errors, int page, boolean finish) {
		
		System.out.println("Se verifica el nro de pagina por la que esta pasando y se validator correspondiente");
		//La cuestion es la siguiente, voy a crear un validador compuesto por un metodos con los siguientes nombres
		// --> validatePaga1(),...,validatePagen(), de esta manera voy a invocar a la validacion correspondiente.
		
	}
	
	protected void postProcessPage(javax.servlet.http.HttpServletRequest request, java.lang.Object command, org.springframework.validation.Errors errors, int page){
		CreateProjectCommand cpc = (CreateProjectCommand)command;
		// Cuando se loguea a usuario se guarda en sesion.
		User user = (User) request.getSession(true).getAttribute("user");
		
   		Project proyecto = new Project();
    	proyecto.setName(cpc.getProjectName());
    	
    	System.out.println("");
    	
    	String latInicial = cpc.getLatInitial().isEmpty()	?AppPropertiesUtil.getInstance().getValue("mygis.project.create.default.latitud", ""): cpc.getLatInitial();
    	String lonInicial = cpc.getLonInitial().isEmpty()	?AppPropertiesUtil.getInstance().getValue("mygis.project.create.default.longitud", ""): cpc.getLonInitial();
    	String zoonInicial = cpc.getZoomInitial().isEmpty()	?AppPropertiesUtil.getInstance().getValue("mygis.project.create.default.zoonInicial", ""): cpc.getZoomInitial();
    	proyecto.setLatitudeInitial(new Double(latInicial));
    	proyecto.setLongitudeInitial(new Double(lonInicial));
    	proyecto.setZoomInitial(new Integer(zoonInicial));
    	proyecto.setFechaCreacion(new Date());

    	IMyGisServices mgs = MyGisServer.getInstance();
    	mgs.createProject(proyecto, user);
		}

} 

